package com.zim.analysis.saika;

import com.zim.analysis.util.FormatUtils;

public class Ctrl8XXX {
    protected static byte[] instructions(String terminal,byte[] body) {
        byte[] _terminal = FormatUtils.strToByte(terminal);
        byte sn = FormatUtils.getSN();
        int size = body.length;
        byte[] _size;
        if (size <= 0xff) {
            _size = new byte[]{0x00, (byte) size};
        } else {
            _size = new byte[]{(byte) (size / 100), (byte) (size % 100)};
        }
        byte[] _attribute = FormatUtils.bytesConcat(
                FormatUtils.bytesConcat(_terminal, new byte[]{sn}), _size);
        byte[] _message = FormatUtils.bytesConcat(_attribute, body);
        byte xy = FormatUtils.bytesOr(_message);
        byte[] _t1 = FormatUtils.bytesConcat(new byte[]{(byte) 0xaa}, _message); // 起始符
        byte[] _t2 = FormatUtils.bytesConcat(_t1, new byte[]{(byte) xy}); // + 校验
        return FormatUtils.bytesConcat(_t2, new byte[]{(byte) 0xaa}); // + 结束符
  
    }
    
    /**
     * 心跳检查
     * @param terminal
     * @return
     * @throws Exception
     */
    public static byte[] ctrl8006(String terminal) throws Exception {
        return instructions(terminal, new byte[]{(byte) 0x80, 0x06});
    }
    
    public static byte[] ctrl8008(String terminal,String address) throws Exception{
        byte[] cmd = new byte[]{(byte) 0x80, 0x08};
        byte[] byteAddress = address.getBytes("utf-8");
        byte[] _addSize = {0x00};
        if (byteAddress != null) {
            int addSize = byteAddress.length;
            _addSize = new byte[]{(byte) addSize};
        }
        byte[] allAddress = FormatUtils.bytesConcat(_addSize, byteAddress);
        byte[] body = FormatUtils.bytesConcat(cmd, allAddress);
        
        return instructions(terminal, body);
    }
    
    public static byte[] ctrl8009(String terminal,boolean bool) throws Exception {
        byte[] body;
        if(bool) {
            body = new byte[]{(byte) 0x80, 0x09,0x01};
        }else {
            body = new byte[]{(byte) 0x80, 0x09,0x00};
        }
        
        return instructions(terminal, body);
    }
    
    public static byte[] ctrl800A(String terminal,boolean bool) throws Exception{
        byte[] body;
        if(bool) {
            body = new byte[]{(byte) 0x80, 0x0A,0x01};
        }else {
            body = new byte[]{(byte) 0x80, 0x0A,0x00};
        }
        
        return instructions(terminal, body);
    }
    
    public static byte[] ctrl800C(String terminal,boolean bool) throws Exception{
        byte[] body;
        if(bool) {
            body = new byte[]{(byte) 0x80, 0x0C,0x01};
        }else {
            body = new byte[]{(byte) 0x80, 0x0C,0x00};
        }
        
        return instructions(terminal, body);
    }
    
    public static byte[] Ctrl800D(String terminal) throws Exception{
        return instructions(terminal, new byte[]{(byte) 0x80, 0x0D});
    }
    
    public static byte[] ctrl800E(String terminal,String pass) throws Exception{
        byte[] cmd = new byte[]{(byte) 0x80, 0x0E};
        byte[] bytePass = pass.getBytes("utf-8");
        byte[] _addSize = {0x00};
        if (bytePass != null) {
            int addSize = bytePass.length;
            _addSize = new byte[]{(byte) addSize};
        }
        byte[] allPass = FormatUtils.bytesConcat(_addSize, bytePass);
        byte[] body = FormatUtils.bytesConcat(cmd, allPass);
        
        return instructions(terminal, body);
    }
    
    public static byte[] ctrl800F(String terminal) throws Exception{
        return instructions(terminal, new byte[]{(byte) 0x80, 0x0F});
    }
    
    public static byte[] ctrl8010(String terminal,int type) throws Exception{
        byte[] body;
        switch (type) {
        case 1:
            body = new byte[]{(byte) 0x80, 0x10,0x01};
            break;
        case 2:
            body = new byte[]{(byte) 0x80, 0x10,0x02};
            break;
        case 9:
            body = new byte[]{(byte) 0x80, 0x10,0x09};
            break;
        case 81:
            body = new byte[]{(byte) 0x80, 0x10,(byte) 0x81};
            break;
        case 82:
            body = new byte[]{(byte) 0x80, 0x10,(byte) 0x82};
            break;
        case 83:
            body = new byte[]{(byte) 0x80, 0x10,(byte) 0x83};
            break;
        case 84:
            body = new byte[]{(byte) 0x80, 0x10,(byte) 0x84};
            break;
        case 85:
            body = new byte[]{(byte) 0x80, 0x10,(byte) 0x85};
            break;
        case 86:
            body = new byte[]{(byte) 0x80, 0x10,(byte) 0x86};
            break;
        case 87:
            body = new byte[]{(byte) 0x80, 0x10,(byte) 0x87};
            break;
        case 88:
            body = new byte[]{(byte) 0x80, 0x10,(byte) 0x88};
            break;
        default:
            body = new byte[]{(byte) 0x80, 0x10,0x00};
            break;
        }
        
        return instructions(terminal, body);
    }
    
    public static byte[] ctrl8011(String terminal,String productKey,String secret) throws Exception{
        byte[] cmd = new byte[]{(byte) 0x80, 0x11};
        
        String secretStr = productKey + secret;
        byte[] secretByte = secretStr.getBytes("utf-8");

        byte[] body = FormatUtils.bytesConcat(cmd, secretByte);
        
        return instructions(terminal, body);
    }
}
